﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;

using System.Data.Linq;
using System.Data.Linq.Mapping;
using Microsoft.Phone.Data.Linq;
using Microsoft.Phone.Data.Linq.Mapping;
using System.Linq;

namespace MetroWashroom.ViewModels
{
    public class PivotModel : INotifyPropertyChanged
    {
        //把地铁线号数传进来
        public PivotModel(int num, string stationName)
        {
            pivotData = new ObservableCollection<PivotData>();
            InitPivotData(num, stationName);
        }

        private void InitPivotData(int num, string stationName)
        {
            //MertroDB mertroDB = new MertroDB("Data Source ='appdata:/MertroDB.sdf';File Mode = read only");

            //在上面的基础上查出来站内的
            IEnumerable<StationTable> listInner = from s in App.AppMertroDB.StationTable
                                                  where s.IsInner == true && s.LineNum == num
                                                  select s;
            //在上面的基础查出来站外的
            IEnumerable<StationTable> listOuter = from s in App.AppMertroDB.StationTable
                                                  where s.IsInner == false && s.LineNum == num
                                                  select s;
            PivotData pivotDataInner = new PivotData();
            pivotDataInner.PivotHeaderName = stationName + "站内";
            pivotDataInner.StationList = new List<Station>();
            if (listInner.Count() > 0)
            {
                foreach (StationTable stationSingle in listInner)
                {
                    Station station = new Station();
                    station.ID = stationSingle.ID;
                    station.LineNum = stationSingle.LineNum;
                    station.StationName = stationSingle.Sation;
                    station.IsInner = stationSingle.IsInner;
                    station.Descible = stationSingle.Descible;
                    pivotDataInner.StationList.Add(station);
                }
            }
            else//人文添加一条数据，用来当此线路没有站内洗手间时作提醒用
            {
                Station station = new Station();
                station.ID = -1;
                station.LineNum = -1;
                station.StationName = "暂无此线路的站内洗手间数据";
                station.IsInner = false;
                station.Descible = "无";
                pivotDataInner.StationList.Add(station);
            }

            PivotData pivotDataOuter = new PivotData();
            pivotDataOuter.PivotHeaderName = stationName + "站外";
            pivotDataOuter.StationList = new List<Station>();
            if (listOuter.Count() > 0)
            {
                foreach (StationTable stationSingle in listOuter)
                {
                    Station station = new Station();
                    station.ID = stationSingle.ID;
                    station.LineNum = stationSingle.LineNum;
                    station.StationName = stationSingle.Sation;
                    station.IsInner = stationSingle.IsInner;
                    station.Descible = stationSingle.Descible;
                    pivotDataOuter.StationList.Add(station);
                }
            }
            else
            {
                Station station = new Station();
                station.ID = -1;
                station.LineNum = -1;
                station.StationName = "暂无此线路的站外洗手间数据";
                station.IsInner = false;
                station.Descible = "无";
                pivotDataOuter.StationList.Add(station);
            }
            pivotData.Add(pivotDataInner);
            pivotData.Add(pivotDataOuter);

            //    //appdata:/这个表明你的数据库是通过xap打包的
            //var list = from s in northwind.Customers
            //           where s.Country == "USA" //查询国籍是美国的客户
            //           select s.CustomerID;
            //this.DemoList.ItemsSource = list.ToList();
        }

        public ObservableCollection<PivotData> pivotData;

        public ObservableCollection<PivotData> PivotDataBind
        {
            get
            {
                return pivotData;
            }
            set
            {
                pivotData = value;
                OnPropertyChanged("PivotDataBind");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
